package com.qf.sclg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qf.sclg.dao.OrderDao;
import com.qf.sclg.entity.Order;
import com.qf.sclg.result.ResponseCode;
import com.qf.sclg.result.ResponseData;
import com.qf.sclg.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Order)表控制层
 *test gitee
 * @author 
 * @since 2021-10-11 16:32:20
 * api启动地址：https://localhost:8080/swagger-ui.html
 */
@Api("预约记录接口")
@RestController
@RequestMapping("order")
public class OrderController {
    /**
     * 服务对象
     */
    @Resource
    private OrderService orderService;

    //注入数据操作层对象
    @Resource
    private OrderDao orderDao;
//对接口进行描述
    @ApiOperation(value = "通过id和客户电话查找预约纪录")
//    required  参数是否必须传
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id",value = "预约纪录的id",dataType = "Integer",required = true),
//            @ApiImplicitParam(name = "tel",value = "客户电话",dataType = "string",required = true)
//    })
//    对接口参数进行描述
//    @ApiImplicitParam(name = "id",value = "预约纪录的id",dataType = "Integer",required = true)
    @ApiImplicitParam(name = "id",value = "返回数据",required = true)
    @GetMapping("/query/{id}")
    public ResponseData queryId(@PathVariable("id") Integer id){
        Order order = orderDao.queryById(id);
        return new ResponseData(order);
    }
    @ApiOperation(value = "查询所有数据")
    @GetMapping ("/all")
    public ResponseData queryAll() throws Exception{
        List<Order> orders = orderService.queryAll();
        return  new ResponseData("0","success",orders);

    }
//    @GetMapping("/query/{id}/{tel}")
//    public Order queryId(@PathVariable("id") Integer id,@PathVariable("tel") String tel){
//        Order order = orderDao.queryById(id);
//        return order;
//    }
//    @ApiIgnore
//    @GetMapping Mapping("/query/{id}/{tel}")
//    public Order queryId(@PathVariable("id") Integer id,@PathVariable("tel") String tel){
//        Order order = orderDao.queryById(id);
//        return order;
//    }
//        @ApiOperation(value = "post测试id")
//        @ApiImplicitParam(name = "id",value = "post测试",dataType = "Integer",required = true)
//        @PostMapping ("/queryyy")
//            public Order queryId1(@PathVariable("id") Integer id){
//                Order order = orderDao.queryById(id);
//                return order;
//            }




    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Order> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.orderService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param order 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Order> add(Order order) {
        return ResponseEntity.ok(this.orderService.insert(order));
    }

    
    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.orderService.deleteById(id));
    }
//    post请求处理
    @ApiOperation(value = "添加预约纪录")
    @PostMapping("/api/insert")
//    前端传入数据，进行拿取
    public ResponseData order(@RequestBody Order order) throws Exception{
        String info = orderService.order(order);
        if(info.equals("success")){
            return new ResponseData();
        }else {
            return new ResponseData(ResponseCode.ERROR);
        }
    }

    @ApiOperation(value = "通过id修改预约记录的状态")
    @PatchMapping("/status")
    public ResponseData updatestatus(@RequestBody Order order) throws Exception{
        Boolean re = orderService.update(order);
        if(re == true){
            return new ResponseData();
        }else {
            return new ResponseData("1009","修改失败");
        }
    }
    @ApiOperation(value = "通过id批量删除数据")
    @DeleteMapping("/dels")
    public ResponseData updateStatus(@RequestBody String ids) {
        JSONObject jsonObject = JSONObject.parseObject(ids);
        JSONArray idArr = (JSONArray) jsonObject.get("ids");
        Boolean res = orderService.deleteByIds(idArr);
        if(res == true){
            return new ResponseData();
        }else{
            return new ResponseData("1010","删除失败,请刷新后重试");
        }
    }
    @ApiOperation(value = "修改订单记录")
    @PatchMapping("/update")
    public ResponseData update(@RequestBody Order order) throws Exception{
        Boolean re = orderService.update(order);
        if(re == true){
            return new ResponseData();
        }else {
            return new ResponseData("1009","修改失败");
        }
    }

    @ApiOperation(value = "搜索")
    @GetMapping("/con")
    public ResponseData queryByCons(Order order){
        List<Order> orders = orderService.queryByConditions(order);
        return new ResponseData("0","success",orders);
    }
}
